/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frontera;

import entidad.Usuario;
import java.awt.Color;
import java.awt.event.ActionEvent;

/**
 *
 * @authors Andrés,Daniel,Osmond
 */
public class VisualizarUsuarios extends javax.swing.JPanel {

    /**
     * Creates new form VisualizarUsuarios
     */
    private AdministrarUsuarios administrarUsuarios;

    public VisualizarUsuarios(AdministrarUsuarios administarUsuarios) {
        initComponents();
        this.administrarUsuarios = administarUsuarios;
        deshabilitarTodo();
        mostrarAviso("");
    }

    public void deshabilitarTodo() {
        nombreTF.setEditable(false);
        cedulaTF.setEditable(false);
        loginTF.setEditable(false);
        contrasenaTF.setEditable(false);
        cargoTF.setEditable(false);

        cProdCK.setEnabled(false);
        cUsuaCK.setEnabled(false);
        cProvCK.setEnabled(false);

        aProdCK.setEnabled(false);
        aProvCK.setEnabled(false);
        aUsuaCK.setEnabled(false);

        bProdCK.setEnabled(false);
        bProvCK.setEnabled(false);
        bUsuaCK.setEnabled(false);

        eProdCK.setEnabled(false);
        eProvCK.setEnabled(false);
        eUsuaCK.setEnabled(false);

        gRepoCK.setEnabled(false);
        rTransCK.setEnabled(false);

        cancelarB.setVisible(false);
        guardarB.setVisible(false);
    }

    public void habilitarTodo() {
        nombreTF.setEditable(true);
        cedulaTF.setEditable(true);
        loginTF.setEditable(true);
        contrasenaTF.setEditable(true);
        cargoTF.setEditable(true);

        cProdCK.setEnabled(true);
        cProvCK.setEnabled(true);
        cUsuaCK.setEnabled(true);


        aProdCK.setEnabled(true);
        aProvCK.setEnabled(true);
        aUsuaCK.setEnabled(true);

        bProdCK.setEnabled(true);
        bProvCK.setEnabled(true);
        bUsuaCK.setEnabled(true);

        eProdCK.setEnabled(true);
        eProvCK.setEnabled(true);
        eUsuaCK.setEnabled(true);

        gRepoCK.setEnabled(true);
        rTransCK.setEnabled(true);

        cancelarB.setVisible(true);
        guardarB.setVisible(true);
        mostrarAviso("");
    }

    /*
     * Modo extra para suplir la necesidad de modificar todo excepto cedula....
     * ya que es nuestro identificador
     */
    public void habilitarModif() {
        habilitarTodo();
        cedulaTF.setEditable(false);
        mostrarAviso("");
    }

    public void visualizar(Usuario usuario) {
        nombreTF.setText(usuario.getNombre());
        cedulaTF.setText("" + usuario.getCedula());
        loginTF.setText(usuario.getNombredeUsuario());
        contrasenaTF.setText(usuario.getContraseña());
        cargoTF.setText(usuario.getCargo());

        cProdCK.setSelected(usuario.getPermisos().puedeConsultarProductos());
        cProvCK.setSelected(usuario.getPermisos().puedeConsultarProveedores());
        cUsuaCK.setSelected(usuario.getPermisos().puedeConsultarUsuarios());

        aProdCK.setSelected(usuario.getPermisos().puedeAgregarProductos());
        aProvCK.setSelected(usuario.getPermisos().puedeAgregarProveedores());
        aUsuaCK.setSelected(usuario.getPermisos().puedeAgregarUsuarios());

        eProdCK.setSelected(usuario.getPermisos().puedeEditarProductos());
        eProvCK.setSelected(usuario.getPermisos().puedeEditarProveedores());
        eUsuaCK.setSelected(usuario.getPermisos().puedeEditarUsuarios());

        bProdCK.setSelected(usuario.getPermisos().puedeEliminarProductos());
        bProvCK.setSelected(usuario.getPermisos().puedeEliminarProveedores());
        bUsuaCK.setSelected(usuario.getPermisos().puedeEliminarUsuarios());

        gRepoCK.setSelected(usuario.getPermisos().puedeGenerarReportes());
        rTransCK.setSelected(usuario.getPermisos().puedeRealizarTransferencias());
    }

    public void limpiar() {
        nombreTF.setText("");
        cedulaTF.setText("");
        loginTF.setText("");
        contrasenaTF.setText("");
        cargoTF.setText("");

        cProdCK.setSelected(false);
        cProvCK.setSelected(false);
        cUsuaCK.setSelected(false);

        aProdCK.setSelected(false);
        aProvCK.setSelected(false);
        aUsuaCK.setSelected(false);

        eProdCK.setSelected(false);
        eProvCK.setSelected(false);
        eUsuaCK.setSelected(false);

        bProdCK.setSelected(false);
        bProvCK.setSelected(false);
        bUsuaCK.setSelected(false);

        gRepoCK.setSelected(false);
        rTransCK.setSelected(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        informacion = new javax.swing.JPanel();
        nombreL = new javax.swing.JLabel();
        nombreTF = new javax.swing.JTextField();
        cedulaL = new javax.swing.JLabel();
        cedulaTF = new javax.swing.JTextField();
        loginL = new javax.swing.JLabel();
        loginTF = new javax.swing.JTextField();
        contrasenaL = new javax.swing.JLabel();
        contrasenaTF = new javax.swing.JPasswordField();
        cargoL = new javax.swing.JLabel();
        cargoTF = new javax.swing.JTextField();
        permisos = new javax.swing.JPanel();
        cUsuaCK = new javax.swing.JCheckBox();
        cProvCK = new javax.swing.JCheckBox();
        cProdCK = new javax.swing.JCheckBox();
        aUsuaCK = new javax.swing.JCheckBox();
        aProvCK = new javax.swing.JCheckBox();
        aProdCK = new javax.swing.JCheckBox();
        eUsuaCK = new javax.swing.JCheckBox();
        eProvCK = new javax.swing.JCheckBox();
        eProdCK = new javax.swing.JCheckBox();
        bUsuaCK = new javax.swing.JCheckBox();
        bProvCK = new javax.swing.JCheckBox();
        bProdCK = new javax.swing.JCheckBox();
        rTransCK = new javax.swing.JCheckBox();
        gRepoCK = new javax.swing.JCheckBox();
        guardarB = new javax.swing.JButton();
        cancelarB = new javax.swing.JButton();
        mensajeL = new javax.swing.JLabel();

        informacion.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Información del usuario", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Segoe UI", 1, 12))); // NOI18N

        nombreL.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        nombreL.setText("Nombre");

        nombreTF.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N

        cedulaL.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        cedulaL.setText("CC");

        cedulaTF.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N

        loginL.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        loginL.setText("Login");

        loginTF.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N

        contrasenaL.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        contrasenaL.setText("Contraseña");

        contrasenaTF.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N

        cargoL.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        cargoL.setText("Cargo");

        cargoTF.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N

        javax.swing.GroupLayout informacionLayout = new javax.swing.GroupLayout(informacion);
        informacion.setLayout(informacionLayout);
        informacionLayout.setHorizontalGroup(
            informacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(informacionLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(informacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cargoL, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(contrasenaL, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 99, Short.MAX_VALUE)
                    .addComponent(loginL, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cedulaL, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(nombreL, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(informacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(nombreTF, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cedulaTF, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(loginTF, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(contrasenaTF, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cargoTF, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        informacionLayout.setVerticalGroup(
            informacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(informacionLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(informacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nombreL)
                    .addComponent(nombreTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(informacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cedulaL)
                    .addComponent(cedulaTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(informacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginL)
                    .addComponent(loginTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(informacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(contrasenaL)
                    .addComponent(contrasenaTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(informacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cargoL)
                    .addComponent(cargoTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        permisos.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Permisos del usuario", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Segoe UI", 1, 12))); // NOI18N
        permisos.setForeground(new java.awt.Color(102, 102, 255));
        permisos.setLayout(new java.awt.GridLayout(7, 2));

        cUsuaCK.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        cUsuaCK.setText("Consultar Usuarios");
        permisos.add(cUsuaCK);

        cProvCK.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        cProvCK.setText("Consultar Proveedores");
        permisos.add(cProvCK);

        cProdCK.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        cProdCK.setText("Consultar Productos");
        permisos.add(cProdCK);

        aUsuaCK.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        aUsuaCK.setText("Agregar Usuarios");
        permisos.add(aUsuaCK);

        aProvCK.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        aProvCK.setText("Agregar Proveedores");
        permisos.add(aProvCK);

        aProdCK.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        aProdCK.setText("Agregar Productos");
        permisos.add(aProdCK);

        eUsuaCK.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        eUsuaCK.setText("Editar Usuarios");
        permisos.add(eUsuaCK);

        eProvCK.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        eProvCK.setText("Editar Proveedores");
        permisos.add(eProvCK);

        eProdCK.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        eProdCK.setText("Editar Productos");
        permisos.add(eProdCK);

        bUsuaCK.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        bUsuaCK.setText("Eliminar Usuarios");
        permisos.add(bUsuaCK);

        bProvCK.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        bProvCK.setText("Eliminar Proveedores");
        permisos.add(bProvCK);

        bProdCK.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        bProdCK.setText("Eliminar Productos");
        permisos.add(bProdCK);

        rTransCK.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        rTransCK.setText("Realizar Transferencias");
        permisos.add(rTransCK);

        gRepoCK.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        gRepoCK.setText("Generar Reportes");
        permisos.add(gRepoCK);

        guardarB.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        guardarB.setText("Guardar");
        guardarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarBActionPerformed(evt);
            }
        });

        cancelarB.setFont(new java.awt.Font("Segoe UI", 0, 12)); // NOI18N
        cancelarB.setText("Cancelar");
        cancelarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarBActionPerformed(evt);
            }
        });

        mensajeL.setFont(new java.awt.Font("Segoe UI Semibold", 0, 14)); // NOI18N
        mensajeL.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        mensajeL.setText("Mensaje");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(guardarB, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancelarB)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(permisos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(informacion, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(mensajeL, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(mensajeL)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(informacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(permisos, javax.swing.GroupLayout.PREFERRED_SIZE, 160, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(guardarB)
                    .addComponent(cancelarB))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cancelarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarBActionPerformed
        limpiar();
        deshabilitarTodo();
        administrarUsuarios.actualizarResultados();

    }//GEN-LAST:event_cancelarBActionPerformed

    private void guardarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarBActionPerformed

        if(!VentanaPrincipal.esLong(cedulaTF.getText())){
            mostrarAviso("El campo cédula solo admite valores numéricos", Color.RED);
            return;
        }
        
        boolean[] permisosUsuario = new boolean[14];
        permisosUsuario[0] = cProdCK.isSelected();
        permisosUsuario[1] = cProvCK.isSelected();
        permisosUsuario[2] = cUsuaCK.isSelected();
        permisosUsuario[3] = aProdCK.isSelected();
        permisosUsuario[4] = aProvCK.isSelected();
        permisosUsuario[5] = aUsuaCK.isSelected();
        permisosUsuario[6] = eProdCK.isSelected();
        permisosUsuario[7] = eProvCK.isSelected();
        permisosUsuario[8] = eUsuaCK.isSelected();
        permisosUsuario[9] = bProdCK.isSelected();
        permisosUsuario[10] = bProvCK.isSelected();
        permisosUsuario[11] = bUsuaCK.isSelected();
        permisosUsuario[12] = gRepoCK.isSelected();
        permisosUsuario[13] = rTransCK.isSelected();
        
        Usuario nuevo = new Usuario(nombreTF.getText(),
                                    Long.parseLong(cedulaTF.getText()),
                                    loginTF.getText(),
                                    new String(contrasenaTF.getPassword()),
                                    cargoTF.getText(), permisosUsuario);

        String mensaje = "";
        if (this.administrarUsuarios.getSeleccionado() == null) {
            mensaje = VentanaPrincipal.controlUsuario.agregar(nuevo);
            if (!mensaje.equals("Usuario agregado exitosamente!")){
                mostrarAviso(mensaje, Color.RED);
                return;
            }
        } else {
            mensaje = VentanaPrincipal.controlUsuario.modificar(nuevo);
            if (!mensaje.equals("Usuario modificado exitosamente!")){
                mostrarAviso(mensaje, Color.RED);
                return;
            }
        }
        
        mostrarAviso(mensaje, Color.BLUE);
        limpiar();
        deshabilitarTodo();
        administrarUsuarios.actualizarResultados();
    }//GEN-LAST:event_guardarBActionPerformed

    public static void mostrarAviso(String mensaje) {
        mensajeL.setText(mensaje);
        mensajeL.setForeground(Color.red);
    }

    public static void mostrarAviso(String mensaje, java.awt.Color s) {
        mensajeL.setText(mensaje);
        mensajeL.setForeground(s);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox aProdCK;
    private javax.swing.JCheckBox aProvCK;
    private javax.swing.JCheckBox aUsuaCK;
    private javax.swing.JCheckBox bProdCK;
    private javax.swing.JCheckBox bProvCK;
    private javax.swing.JCheckBox bUsuaCK;
    private javax.swing.JCheckBox cProdCK;
    private javax.swing.JCheckBox cProvCK;
    private javax.swing.JCheckBox cUsuaCK;
    private javax.swing.JButton cancelarB;
    private javax.swing.JLabel cargoL;
    private javax.swing.JTextField cargoTF;
    private javax.swing.JLabel cedulaL;
    private javax.swing.JTextField cedulaTF;
    private javax.swing.JLabel contrasenaL;
    private javax.swing.JPasswordField contrasenaTF;
    private javax.swing.JCheckBox eProdCK;
    private javax.swing.JCheckBox eProvCK;
    private javax.swing.JCheckBox eUsuaCK;
    private javax.swing.JCheckBox gRepoCK;
    private javax.swing.JButton guardarB;
    private javax.swing.JPanel informacion;
    private javax.swing.JLabel loginL;
    private javax.swing.JTextField loginTF;
    private static javax.swing.JLabel mensajeL;
    private javax.swing.JLabel nombreL;
    private javax.swing.JTextField nombreTF;
    private javax.swing.JPanel permisos;
    private javax.swing.JCheckBox rTransCK;
    // End of variables declaration//GEN-END:variables
}
